﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

using CE.Global;
using CE.User;
using CE.Database;
using CE.CarInsurance;

public partial class car_insurance_usermessage : MyLanguageBasePage
{
    protected string _header;
    protected string _body;
    protected string _headerMsg;
    protected string _bodyMsg;

    //protected override void InitializeCulture()
    //{
    //    String selectedCulture = CE.Global.General.SetCulture();
    //    //UICulture = selectedCulture;
    //    Culture = selectedCulture;
    //    base.InitializeCulture();
    //}
    string _redirectUrl = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            string _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/login.aspx", Page);
            Response.Redirect(_redirectUrl);
        }
        if (!IsPostBack)
        {
            //HyperLink _lnkBacktoHome = new HyperLink();
            //imgBacktoHome.Attributes.Add("onmouseover", "this.src='" + _lnkBacktoHome.ResolveClientUrl(Resources.ImageResource.BacktoHomeGreenUrl) + "';");
            //imgBacktoHome.Attributes.Add("onmouseout", "this.src='" + _lnkBacktoHome.ResolveClientUrl(Resources.ImageResource.BacktoHomeBlueUrl) + "';");

            //HyperLink _lnkPrint = new HyperLink();
            //hplprint.Attributes.Add("onmouseover", "this.src='" + _lnkPrint.ResolveClientUrl(Resources.ImageResource.PrintHoverUrl) + "';");
            //hplprint.Attributes.Add("onmouseout", "this.src='" + _lnkPrint.ResolveClientUrl(Resources.ImageResource.PrintUrl) + "';");

            string ProviderId, referencenum, emailadd, CarId, Bank;
            ProviderId = "0"; referencenum = ""; emailadd = ""; CarId = "0"; Bank = "";

            /* //Sakthi on 21 Jun 2011 -- start
            if (Request.QueryString["ref"] != null)
            {
                CarId = GetCarId(Request.QueryString["ref"].ToString());
            }
            if (Request.QueryString["pcid"] != null)
            //Sakthi on 21 Jun 2011 -- End */
            if (Session["CIMSG"] != null)
            {
                divMsgType2.Visible = true;
                string[] val = Session["CIMSG"].ToString().Split(',');
                CarId = GetCarId(val[1].ToString());
                ProviderId = Decode(val[0]).ToString();
                if (val.Length > 2)
                {
                    Bank = val[2].ToString();
                }
                //referencenum = Request.QueryString["ref"].ToString(); //Sakthi on 21 Jun 2011
                referencenum = val[0].ToString();
                emailadd = "emailaddress@insurancecompany.com.th";//CE.Configuration.MyConfigurationManager.GetAppSettingValue("HSBCCI_UserMessageEmail");

                GetMessage(400, "CI", 23100);
                _headerMsg = string.Format(_header);
                _bodyMsg = string.Format(_body, referencenum, emailadd, emailadd);
                lblMessageHeader.Text = _headerMsg;
                lblMessageBody.Text = _bodyMsg;
            }
            else
            {
                divMsgType2.Visible = false;
            }
            if (CarId != "0" && ProviderId != "0")
            {
                LoadCarPolicyDetails(Convert.ToInt64(CarId.ToString()), Convert.ToInt64(ProviderId.ToString()));
            }

        }
    }

    private void GetMessage(Int32 UserTypeId, string MessageType, Int32 ErrorCode)
    {
        MessageDB _messagedb = new MessageDB();
        DataTable _Message = new DataTable();
        _Message = _messagedb.GetMessage(UserTypeId, MessageType, ErrorCode);
        if (_Message != null && _Message.Rows.Count > 0)
        {
            if (System.Globalization.CultureInfo.CurrentCulture.Name == "th-TH")
            {
                _header = _Message.Rows[0]["THHeader"].ToString();
                _body = _Message.Rows[0]["THBody"].ToString();
            }
            else
            {
                _header = _Message.Rows[0]["Header"].ToString();
                _body = _Message.Rows[0]["Body"].ToString();
            }
        }
        else
        {
            _header = "GluayGluay.com";
            _body = "No message found!";
        }
    }

    public string GetCarId(string RefNo)
    {
        string _result = "";
        string val = RefNo.Substring(1, RefNo.Length - 1);
        string[] words = new string[val.Length];
        char[] cwords = val.ToCharArray();
        for (int i = 0; i < cwords.Length; i++)
        {
            _result += GetRefNo(cwords[i]);
        }
        return _result;
    }

    public string GetRefNo(char C)
    {
        string _result = "";
        double Num;
        bool isNum = double.TryParse(C.ToString(), out Num);
        if (isNum)
        {
            _result = Num.ToString();
        }
        else
        {
            if (C == 'A' || C == 'a')
                _result = "1";
            else if (C == 'B' || C == 'b')
                _result = "2";
            else if (C == 'C' || C == 'c')
                _result = "3";
            else if (C == 'D' || C == 'd')
                _result = "4";
            else if (C == 'E' || C == 'e')
                _result = "5";
            else if (C == 'F' || C == 'f')
                _result = "6";
            else if (C == 'G' || C == 'g')
                _result = "7";
            else if (C == 'H' || C == 'h')
                _result = "8";
            else if (C == 'I' || C == 'i')
                _result = "9";
            else if (C == 'J' || C == 'j')
                _result = "0";
        }
        return _result;
    }

    public string Decode(string sData)
    {
        try
        {
            System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
            System.Text.Decoder utf8Decode = encoder.GetDecoder();
            byte[] todecode_byte = Convert.FromBase64String(sData);
            int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
            char[] decoded_char = new char[charCount];
            utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
            string result = new String(decoded_char);
            return result;
        }
        catch (Exception ex)
        {
            Response.Redirect("~/");
            string result = "";
            return result;
        }
    }

    public void LoadCarPolicyDetails(Int64 CECarId, Int64 ProviderCompanyId)
    {
        Int64 UserId = 0;
        Int64 CXCTypeId = 0;
        MyDatabase _db = new MyDatabase();
        string TableName = GetCarMoreDetailsTableName(ProviderCompanyId);
        string strSql = "SELECT CarResultId,RefCEUserId FROM " + TableName + " WHERE CECarId  = " + CECarId;
        DataTable _dtResult = _db.GetDataTable(strSql);
        if (_dtResult.Rows.Count > 0)
        {
            CXCTypeId = Convert.ToInt64(_dtResult.Rows[0]["CarResultId"].ToString());
            UserId = Convert.ToInt64(_dtResult.Rows[0]["RefCEUserId"].ToString());
        }
        Boolean _isCTPL = false;
        DataSet _dtProvider = new DataSet();
        if (ProviderCompanyId == 11)
        {
            AZCpCarMoreDetailDB _azcpcarmoredetaildb = new AZCpCarMoreDetailDB();
            _dtProvider = _azcpcarmoredetaildb.GetCarPolicyDetails(CECarId, UserId, CXCTypeId, ProviderCompanyId);
            _isCTPL = _azcpcarmoredetaildb.GetCarMoreDetail(CECarId, UserId).IsCTPL;
        }
        else if (ProviderCompanyId == 12)
        {
            MSIGCarMoreDetailDB _msigcarmoredetaildb = new MSIGCarMoreDetailDB();
            _dtProvider = _msigcarmoredetaildb.GetCarPolicyDetails(CECarId, UserId, CXCTypeId, ProviderCompanyId);
            _isCTPL = _msigcarmoredetaildb.GetCarMoreDetail(CECarId, UserId).IsCTPL;
        }
        else if (ProviderCompanyId == 16)
        {
            AXACarMoreDetailDB _axacarmoredetaildb = new AXACarMoreDetailDB();
            _dtProvider = _axacarmoredetaildb.GetCarPolicyDetails(CECarId, UserId, CXCTypeId, ProviderCompanyId);
            _isCTPL = _axacarmoredetaildb.GetCarMoreDetail(CECarId, UserId).IsCTPL;
        }
        else if (ProviderCompanyId == 29)
        {
            LMGCarMoreDetailDB _lmgcarmoredetaildb = new LMGCarMoreDetailDB();
            _dtProvider = _lmgcarmoredetaildb.GetCarPolicyDetails(CECarId, UserId, CXCTypeId, ProviderCompanyId);
            _isCTPL = _lmgcarmoredetaildb.GetCarMoreDetail(CECarId, UserId).IsCTPL;
        }
        else if (ProviderCompanyId == 31)
        {
            ChartisCarMoreDetailDB _chartiscarmoredetaildb = new ChartisCarMoreDetailDB();
            _dtProvider = _chartiscarmoredetaildb.GetCarPolicyDetails(CECarId, UserId, CXCTypeId, ProviderCompanyId);
            _isCTPL = _chartiscarmoredetaildb.GetCarMoreDetail(CECarId, UserId).IsCTPL;
        }
        else if (ProviderCompanyId == 32)
        {
            BKICarMoreDetailDB _bkicarmoredetaildb = new BKICarMoreDetailDB();
            _dtProvider = _bkicarmoredetaildb.GetCarPolicyDetails(CECarId, UserId, CXCTypeId, ProviderCompanyId);
            _isCTPL = _bkicarmoredetaildb.GetCarMoreDetail(CECarId, UserId).IsCTPL;
        }
        else if (ProviderCompanyId == 33)
        {
            MTICarMoreDetailDB _mticarmoredetaildb = new MTICarMoreDetailDB();
            _dtProvider = _mticarmoredetaildb.GetCarPolicyDetails(CECarId, UserId, CXCTypeId, ProviderCompanyId);
            _isCTPL = _mticarmoredetaildb.GetCarMoreDetail(CECarId, UserId).IsCTPL;
        }
        else if (ProviderCompanyId == 35)
        {
            KPICarMoreDetailDB _kpicarmoredetaildb = new KPICarMoreDetailDB();
            _dtProvider = _kpicarmoredetaildb.GetCarPolicyDetails(CECarId, UserId, CXCTypeId, ProviderCompanyId);
            _isCTPL = _kpicarmoredetaildb.GetCarMoreDetail(CECarId, UserId).IsCTPL;
        }
        else if (ProviderCompanyId == 39) //Mittare
        {
            MittareCarMoreDetailDB _mittarecarmoredetaildb = new MittareCarMoreDetailDB();
            _dtProvider = _mittarecarmoredetaildb.GetCarPolicyDetails(CECarId, UserId, CXCTypeId, ProviderCompanyId);
            _isCTPL = _mittarecarmoredetaildb.GetCarMoreDetail(CECarId, UserId).IsCTPL;
        }
        else if (ProviderCompanyId == 41)
        {
            KurniaCarMoreDetailDB _kurniacarmoredetaildb = new KurniaCarMoreDetailDB();
            _dtProvider = _kurniacarmoredetaildb.GetCarPolicyDetails(CECarId, UserId, CXCTypeId, ProviderCompanyId);
            _isCTPL = _kurniacarmoredetaildb.GetCarMoreDetail(CECarId, UserId).IsCTPL;
        }
        else if (ProviderCompanyId == 43)
        {
            SMKCarMoreDetailDB _smkcarmoredetaildb = new SMKCarMoreDetailDB();
            _dtProvider = _smkcarmoredetaildb.GetCarPolicyDetails(CECarId, UserId, CXCTypeId, ProviderCompanyId);
            _isCTPL = _smkcarmoredetaildb.GetCarMoreDetail(CECarId, UserId).IsCTPL;
        }
        else if (ProviderCompanyId == 45)
        {
            ViriyahCarMoreDetailDB _viriyahcarmoredetaildb = new ViriyahCarMoreDetailDB();
            _dtProvider = _viriyahcarmoredetaildb.GetCarPolicyDetails(CECarId, UserId, CXCTypeId, ProviderCompanyId);
            _isCTPL = _viriyahcarmoredetaildb.GetCarMoreDetail(CECarId, UserId).IsCTPL;
        }
        else if (ProviderCompanyId == 46)
        {
            TMSCarMoreDetailDB _tmscarmoredetaildb = new TMSCarMoreDetailDB();
            _dtProvider = _tmscarmoredetaildb.GetCarPolicyDetails(CECarId, UserId, CXCTypeId, ProviderCompanyId);
            _isCTPL = _tmscarmoredetaildb.GetCarMoreDetail(CECarId, UserId).IsCTPL;
        }
        else if (ProviderCompanyId == 50)
        {
            SCSMGCarMoreDetailDB _scsmgcarmoredetaildb = new SCSMGCarMoreDetailDB();
            _dtProvider = _scsmgcarmoredetaildb.GetCarPolicyDetails(CECarId, UserId, CXCTypeId, ProviderCompanyId);
            _isCTPL = _scsmgcarmoredetaildb.GetCarMoreDetail(CECarId, UserId).IsCTPL;
        }
        else if (ProviderCompanyId == 58)
        {
            TVVCarMoreDetailDB _tvvcarmoredetaildb = new TVVCarMoreDetailDB();
            _dtProvider = _tvvcarmoredetaildb.GetCarPolicyDetails(CECarId, UserId, CXCTypeId, ProviderCompanyId);
            _isCTPL = _tvvcarmoredetaildb.GetCarMoreDetail(CECarId, UserId).IsCTPL;
        }

        if (_dtProvider != null && _dtProvider.Tables.Count > 0)
        {
            if (_dtProvider.Tables[0].Rows.Count > 0 && _dtProvider.Tables[1].Rows.Count > 0 && _dtProvider.Tables[2].Rows.Count > 0)
            {
                if (_dtProvider.Tables[1].Rows[0]["PlanName"].ToString() != "GluayGluay.com Special")
                {
                    lblSelectInsurer.Text = _dtProvider.Tables[1].Rows[0]["CompanyName"].ToString();
                    lblPackages.Text = _dtProvider.Tables[1].Rows[0]["PlanName"].ToString();
                }
                else
                {
                    lblInsurance.Text = Resources.ProposalForm.Package;
                    lblSelectInsurer.Text = _dtProvider.Tables[1].Rows[0]["PlanName"].ToString();
                    lblPackageName.Text = Resources.ProposalForm.PackageCondition;
                    lblPackages.Text = Resources.CarInsuranceResultPage.SpecialNCB;
                }
                
                lblCarMakemodel.Text = _dtProvider.Tables[0].Rows[0]["MakeModelDesc"].ToString();

                lblCarModelYear.Text = _dtProvider.Tables[0].Rows[0]["ManufactYear"].ToString();
                float _InsuredAmt = Convert.ToSingle(_dtProvider.Tables[1].Rows[0]["SumInsured"].ToString());
                if (_InsuredAmt > 0)
                {
                    lblSumInsured.Text = String.Format("{0:0,0}", _InsuredAmt) + " " + Resources.CarInsurance.Baht;  //String.Format("฿{0:0,0.00}", _InsuredAmt);
                }
                else
                {
                    lblSumInsured.Text = "N/A";
                }                
                //lblSumInsured.Text = String.Format("{0:0,0}", _InsuredAmt) + " " + Resources.CarInsurance.Baht;//String.Format("฿{0:0,0.00}", _InsuredAmt);
                //float _APremium = Convert.ToSingle(_dtProvider.Tables[1].Rows[0]["AnnualPremium"].ToString());//Hide for Sop Request on 12 Sep 2011 by Skype
                float _APremium = Convert.ToSingle(_dtProvider.Tables[1].Rows[0]["PromotionPrice"].ToString());//Change for Sop Request on 12 Sep 2011 by Skype
                lblPremiums.Text = String.Format("{0:0,0.00}", _APremium) + " " + Resources.CarInsurance.Baht;//String.Format("฿{0:0,0.00}", _APremium);

                //float _VarPremium = Convert.ToSingle(_dtProvider.Tables[1].Rows[0]["AnnualPremium"].ToString());//Hide for Sop Request on 12 Sep 2011 by Skype
                float _VarPremium = Convert.ToSingle(_dtProvider.Tables[1].Rows[0]["PromotionPrice"].ToString());//Change for Sop Request on 12 Sep 2011 by Skype
                float _VarCtpl = Convert.ToSingle(_dtProvider.Tables[1].Rows[0]["CTPL"].ToString());
                
                if (_isCTPL)
                {
                    float _VarTotalPremiumPlusCtpl = _VarPremium + _VarCtpl;
                    lblTotalPremium.Text = String.Format("{0:0,0.00}", _VarTotalPremiumPlusCtpl) + " " + Resources.CarInsurance.Baht;  // String.Format("฿{0:0,0.00}", _VarTotalPremiumPlusCtpl);
                }
                else
                {
                    lblTotalPremium.Text = String.Format("{0:0,0.00}", _VarPremium) + " " + Resources.CarInsurance.Baht;  // String.Format("฿{0:0,0.00}", _VarTotalPremiumPlusCtpl);
                }

                lblCommencementdate.Text = "";
                if (_dtProvider.Tables[0].Rows[0]["CommenceDate"].ToString() != "")
                {
                    lblCommencementdate.Text = Convert.ToDateTime(_dtProvider.Tables[0].Rows[0]["CommenceDate"]).ToString("dd-MMM-yyyy");
                }

                string convertno = General.GetRefNumber(CECarId);
                if (_dtProvider.Tables[1].Rows[0]["PlanName"].ToString() != "GluayGluay.com Special")
                {
                    if (ProviderCompanyId == 11)
                    {
                        lblQRefNo.Text = "AZC" + convertno.ToString();
                    }
                    else if (ProviderCompanyId == 12)
                    {
                        lblQRefNo.Text = "MSI" + convertno.ToString();
                    }
                    else if (ProviderCompanyId == 16)
                    {
                        lblQRefNo.Text = "AXA" + convertno.ToString();
                    }
                    else if (ProviderCompanyId == 29)
                    {
                        lblQRefNo.Text = "LMG" + convertno.ToString();
                    }
                    else if (ProviderCompanyId == 31)
                    {
                        lblQRefNo.Text = "CTS" + convertno.ToString();
                    }
                    else if (ProviderCompanyId == 32)
                    {
                        lblQRefNo.Text = "BKI" + convertno.ToString();
                    }
                    else if (ProviderCompanyId == 33)
                    {
                        lblQRefNo.Text = "MTI" + convertno.ToString();
                    }
                    else if (ProviderCompanyId == 35)
                    {
                        lblQRefNo.Text = "KPI" + convertno.ToString();
                    }
                    else if (ProviderCompanyId == 39)  //Mittare
                    {
                        lblQRefNo.Text = "MIR" + convertno.ToString();
                    }
                    else if (ProviderCompanyId == 41)
                    {
                        lblQRefNo.Text = "KSK" + convertno.ToString();
                    }
                    else if (ProviderCompanyId == 43)  //Syn Mun Kong
                    {
                        lblQRefNo.Text = "SMK" + convertno.ToString();
                    }
                    else if (ProviderCompanyId == 45)  //Viriyah
                    {
                        lblQRefNo.Text = "VRY" + convertno.ToString();
                    }
                    else if (ProviderCompanyId == 46)
                    {
                        lblQRefNo.Text = "TMS" + convertno.ToString();
                    }
                    else if (ProviderCompanyId == 50)
                    {
                        lblQRefNo.Text = "SCS" + convertno.ToString();
                    }
                    else if (ProviderCompanyId == 58)
                    {
                        lblQRefNo.Text = "TVV" + convertno.ToString();
                    }
                    else if (ProviderCompanyId == 65)
                    {
                        lblQRefNo.Text = "DPY" + convertno.ToString();
                    }
                }
                else
                {
                    lblQRefNo.Text = "GG" + ProviderCompanyId.ToString() + convertno.ToString();
                }
            }
        }
    }

    protected void imgbtnBack_Click(object sender, EventArgs e)
    {

            _redirectUrl = CE.Global.General.GetUrl("/home.aspx", Page);
            Response.Redirect(_redirectUrl);
        

    }

    public string GetCarMoreDetailsTableName(Int64 ProviderId)
    {
        string _TableName = "";
        if (ProviderId == 11)
        {
            _TableName = "tblCarMoreDetail_AZCp";
        }
        else if (ProviderId == 12)
        {
            _TableName = "tblCarMoreDetail_MSIG";
        }
        else if (ProviderId == 16)
        {
            _TableName = "tblCarMoreDetail_AXA";
        }
        else if (ProviderId == 29)
        {
            _TableName = "tblCarMoreDetail_LMG";
        }
        else if (ProviderId == 31)
        {
            _TableName = "tblCarMoreDetail_Chartis";
        }
        else if (ProviderId == 32)
        {
            _TableName = "tblCarMoreDetail_BKI";
        }
        else if (ProviderId == 33)
        {
            _TableName = "tblCarMoreDetail_MTI";
        }
        else if (ProviderId == 35)
        {
            _TableName = "tblCarMoreDetail_KPI";
        }
        else if (ProviderId == 39)
        {
            _TableName = "tblCarMoreDetail_Mittare";
        }
        else if (ProviderId == 41)
        {
            _TableName = "tblCarMoreDetail_Kurnia";
        }
        else if (ProviderId == 43)
        {
            _TableName = "tblCarMoreDetail_SMK";
        }
        else if (ProviderId == 45)
        {
            _TableName = "tblCarMoreDetail_Viriyah";
        }
        else if (ProviderId == 46)
        {
            _TableName = "tblCarMoreDetail_TMS";
        }
        else if (ProviderId == 50)
        {
            _TableName = "tblCarMoreDetail_SCSMG";
        }
        else if (ProviderId == 58)
        {
            _TableName = "tblCarMoreDetail_TVV";
        }
        return _TableName;
    }

}
